package wfmanage.action;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Lookup;

public class AdminAssignRole extends ActionSupport{
	private String domain;
	private String role;
	private String commandButton = "";
	private String WFNames="(";
	ArrayList<Lookup> WFPoints = new ArrayList<Lookup>();
	int domain_id=1;
	int WorkflowPoint_id=1;
	private String workflowpoint_name="";
	int role_id=1;
	private String role_name="";
	private int duration=1;
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	private String msg="";
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String execute() throws Exception {
		 MyLog.log("Inside AdminDefineWF : Method : execute");

	        HttpServletRequest request=(HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
	 
	        Enumeration paramNames= request.getParameterNames();
	 
	        while(paramNames.hasMoreElements())
	        {
	            String param=(String)paramNames.nextElement();
	            MyLog.log("\nParameter Name : "+param);
	            MyLog.log("Parameter Values : ");
	             if(param.equals("myDomain")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup domain=Lookup.findOne("domain", "where domain_name = '"+paramVal+"'");
	 	            	domain_id=domain.getId();
	 	                MyLog.log(paramVal);
	 	            }
	             }
	             else if(param.equals("myWFPoint")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
		 	            for(String paramVal:paramValues)
		 	            {
		 	            	Lookup WFPoint=Lookup.findOne("workflowpoint", "where workflowpoint_name = '"+paramVal+"'");
		 	            	WorkflowPoint_id=WFPoint.getId();
		 	            	workflowpoint_name=WFPoint.getName();
		 	                MyLog.log(paramVal);
		 	            }
	 	            	
	             }
	             else if(param.equals("myRole")){
	            	 String[] paramValues=request.getParameterValues(param);
		             
	 	            for(String paramVal:paramValues)
	 	            {
	 	            	Lookup role=Lookup.findOne("role", "where role_name = '"+paramVal+"'");
	 	            	role_id=role.getId();
	 	            	role_name=role.getName();
	 	                MyLog.log(paramVal);
	 	            }
	             }
	             
	        }
	        if (this.commandButton.startsWith("Assign")) {
	        	 int result=insert();
	           		 if(result>0){
	           			 msg="Assigned workflowpoint "+workflowpoint_name +" successfully to role "+role_name+"!!";
	           			 MyLog.log("Assigned workflowpoint successfully to role");
	           		 }
	           		 else{
	           			msg="Cannot Assign, Workflow Point "+workflowpoint_name +" already tagged to the role "+role_name;
	           			 MyLog.log("cannot assign, workflowpoint already tagged to this role");
	           			 return "displayWF";
	           		 }
          	 	
          	 return "displayWF";
           }
           else if (this.commandButton.startsWith("Remove")) {
          	  	int result=Lookup.findOnefordelete("Domain_WorkflowPoint_Role"," where domain_id="+domain_id+" and WorkflowPoint_id="+WorkflowPoint_id+" and role_id="+role_id);
          	  if(result>0){
          		  	msg="Removed role "+role_name +" successfully from workflow point "+workflowpoint_name +"!!";
        			 MyLog.log("Removed role successfully from workflow point");
        		 }
        		 else{
        			msg="Cannot Remove, Workflow Point "+workflowpoint_name +" tagged to the role "+role_name +". Please try again!!";
        			MyLog.log("cannot assign, workflowpoint already tagged to this role");
        			 return "displayWF";
        		 }
          	return "displayWF";
           }
            else if (this.commandButton.startsWith("Back to Admin")) {
      			return "admin_home";
      		}
	
	    	return "error";
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public int insert() {
		String insertSQL = "insert into Domain_WorkflowPoint_Role(domain_id,WorkflowPoint_id,role_id,duration) values" +
				"				("+domain_id+","+WorkflowPoint_id +", "+role_id+","+duration+");";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
}
